/*
 * @Description:
 * @Author: py
 * @Date: 2024-08-09
 */
import type { OptionsRow, PlusColumn } from 'plus-pro-components'

import { businessList } from '@fl/api/city-service'

import { useDicts } from './use-dicts'

const { getDictOptions } = useDicts()

const aSaleStatusOptions = getDictOptions('oAfterSaleStatus')
const orderStatusOptions = getDictOptions('oOrderStatus', true)
const companyList: Ref<OptionsRow[]> = ref([])
const searchLoading = ref(false)
// const timer = ref<ReturnType<typeof setTimeout> | null>(null)
async function getBusinessList(companyName?: string | undefined) {
    searchLoading.value = true
    await businessList({
        companyName,
    }).then((res) => {
        companyList.value = res.map((resItem) => {
            return {
                label: resItem.companyName,
                value: resItem.companyId,
            }
        })
    })
    searchLoading.value = false
}
getBusinessList()

// function remoteMethod(query: string) {
//     // 防抖
//     if (timer.value) {
//         clearTimeout(timer.value)
//     }
//     timer.value = setTimeout(() => {
//         getBusinessList(query)
//     }, 500)
// }

export function useForm() {
    const schema: PlusColumn[] = [
        {
            colProps: {
                span: 5,
            },
            fieldProps: {
                filterable: true,
                // loading: searchLoading.value,
                // remote: true,
                // multiple: true,
                // remoteMethod,
                // remoteShowSuffix: true,
            },
            label: '商家名称',
            options: computed(() => companyList.value),
            prop: 'companyId',
            valueType: 'select',
        },
        {
            colProps: {
                span: 6,
            },
            label: '商品名称',
            prop: 'goodsName',
            valueType: 'copy',
        },
        {
            colProps: {
                span: 6,
            },
            label: '订单号',
            prop: 'orderCode',
            valueType: 'copy',
        },
        {
            colProps: {
                span: 7,
            },
            fieldProps: {
                endPlaceholder: '结束日期',
                format: 'YYYY-MM-DD',
                startPlaceholder: '开始日期',
                type: 'daterange',
                valueFormat: 'YYYY-MM-DD',
            },
            label: '下单日期',
            prop: 'orderDate',
            valueType: 'date-picker',
        },
        {
            colProps: {
                span: 5,
            },
            label: '售后状态',
            options: aSaleStatusOptions,
            prop: 'orderAfterSaleStatus',
            valueType: 'select',
        },
        {
            colProps: {
                span: 12,
            },
            label: '订单状态',
            options: orderStatusOptions,
            prop: 'orderStatus',
            valueType: 'radio',
        },
    ]

    const state = {
        companyId: '',
        goodsName: '',
        orderAfterSaleStatus: '',
        orderCode: '',
        orderDate: null,
        orderStatus: '',
    }
    const form = ref({
        ...state,
    })
    return { form, schema, state }
}
